<?php
/**
 * Created by PhpStorm
 * User: lcz
 * Date: 2019/10/8
 * Time: 11:54
 * 定时任务
 */
namespace app\common\controller;

use app\common\library\Redis;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;


class Task extends Command {
	protected function configure()
	{
		$this->addArgument('m', Argument::REQUIRED);
		$this->setName('task')->setDescription('定时任务');
	}
	
	protected function execute(Input $input, Output $output)
	{
		$method = $input->getArgument('m');
		$this->$method();
	}
	
	/**
	 * 消息队列
	 */
	public function queue(){
		$redis = Redis::getInstance();
		$config = config('redis');
		$key = $config['prefix'].$config['name']['queue'];
		while (true) {
			$item = $redis->lPop($key);
			if($item) {
				$item = json_decode($item, true);
				$method = $item['method'];
				$obj = new Queue();
				
				$res = $obj->$method($item['data']);
				$str =  $method.'处理结果'.$res;
			} else {
				$str = null;
			}
		
			if($str) {
				$dir = RUNTIME_PATH.DS.'crontabLog'.DS.'queue'.DS;
				if(!is_dir($dir)) {
					mkdir($dir, '0777', true);
				}
				
				$file = $dir.date('Y-m-d').'.txt';
				file_put_contents($file, $str.PHP_EOL, FILE_APPEND);
			}
			
			echo $str;
		}
		
	}
}